<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class UserPay extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
//        $where[]  = ['up.user_id', '<>', 1];

        if(isset($post['out_trade_no']) && !empty($post['out_trade_no'])){
            $where[] = ['out_trade_no', 'like', '%'.$post['out_trade_no'].'%'];
        }
        if(isset($post['name']) && !empty($post['name'])){
            $where[] = ['u.name', 'like', '%'.$post['name'].'%'];
        }
        if(isset($post['pay_user']) && !empty($post['pay_user'])){
            $where[] = ['pay_user', 'like', '%'.$post['pay_user'].'%'];
        }
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['username', 'like', '%'.$post['username'].'%'];
        }
        if(isset($post['pay_status']) && strlen($post['pay_status']) >0){
            $where[] = ['status_check', '=', $post['pay_status']];
        }

        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['up.ctime', 'between', "{$start_time},{$end_time}"];
        }

        $list = Db::name('user_pay')->alias('up')
            ->leftJoin('user u' , 'u.id=up.user_id')
            ->field('u.username,u.name,up.*')
            ->order('up.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}
